
package easim.examples;

import easim.net.Node;
import easim.net.DtnNode;
import easim.net.Clock;
import easim.net.Timer;
import easim.mobi.Mobility;
import easim.mobi.RandomWaypoint;

import jist.swans.Constants;
import jist.swans.misc.Message;

public class Node3 extends DtnNode
{

  static class Message1 implements Message
  {
    public int getSize() {
      return 1024;
    }
    public void getBytes(byte[] msg, int offset) {
      throw new RuntimeException("no implementation.");
    }
  }

  class Timer1 implements Timer
  {
    public void start() {
      Clock.start(this, 2 * Constants.SECOND, false); // schedule a timeout event.
    }
    public void timeout() {
      if (toSend == null) {
        toSend = new Message1(); // generate a new message.
      }
      start(); // schedule the next timeout event.
    }
  }

  public int sent = 0;
  public int received = 0;
  public Message toSend;
  public easim.mobi.Mobility mobility;

  public Node3(int id) {
    super(id);
    mobility = new RandomWaypoint(this);
    new Timer1().start();
  }

  public Message nextMessageToSend() {
    Message message = null;
    if (contacts.size() > 0) { // neighbors exist.
      message = toSend;
      toSend = null;
      if (message != null) {
        ++ sent;
      }
    }
    return message;
  }

  public void receiveMessage(Message message, int srcId) {
    ++ received;
  }

  public void onAckMessage(Message message) {
  }

  public void onDropMessage(Message message, int[] dstIds) {
    System.out.print("Node " + id + " failed to receive acks from its neighbors:\r\n ");
    for (int i = 0; i < dstIds.length; ++ i) {
      System.out.print(dstIds[i] + " ");
    }
    System.out.println("for the last msg it sent.");
  }

  public void onContactDetected(int id) {
    System.out.println(this.id + " connects to " + id);
  }

  public void onContactBroken(int id) {
    System.out.println(this.id + " disconnects to " + id);
  }

  public String toString() {
    return 
      "I sent " + sent + " msgs.\r\n" +
      "I received " + received + " msgs.";
  }

}
